package com.apestech.framework.run.callback;

import java.util.Vector;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.run.core.DBExec;
import com.apestech.framework.util.DBUtil;

public abstract class TransactionThreadCallBack implements ThreadCallBackHandler {
	
	private String dbName;
	private Vector<Object> param;
	
	public TransactionThreadCallBack(String dbName, Vector<Object> param){
		this.dbName=dbName;
		this.param=param;
	}

	@Override
	public void run() throws Exception {
		DBExec db=null;
		try{
			db=new DBExec(DBUtil.getDataSource(dbName));
			db.setAutoCommit(false);
			run(db.getSession(),this.param);
			db.commit();
		} catch(Exception e){
			db.rollback();
			throw e;
		} finally {
			db.close();
            db=null;
		}
	}
	
	protected abstract void run(SqlSession cn, Vector<Object> param) throws Exception;
	
}
